<?php

namespace app\admin\controller;

use app\common\model\MatchesScheduleModel;
use app\common\NStatus;
use think\facade\Request;

class RecommendController extends BaseController
{
    public function index()
    {
        $comp = MatchesScheduleModel::field("comp")->group("comp")->order("match_time", "desc")->select();
        return view("match", [
            "sports"     => NStatus::Sports,
            "all_status" => NStatus::AllStatus,
            "comp"       => $comp,
        ]);
    }

    public function lists()
    {
        $model = new MatchesScheduleModel;

        if ($match_id = trim($this->get('match_id', ''))) {
            $model = $model->where('match_id', $match_id);
        }

        if ($sport_id = trim($this->get('sport_id', ''))) {
            if ($sport_id == 101) {
                $model = $model->whereIn('sport_id', [101, 102, 103]);
            } else {
                $model = $model->where('sport_id', $sport_id);
            }
        }

        if ($comp = trim($this->get('comp', ''))) {
            $model = $model->where('comp', $comp);
        }

        if (in_array($flag = $this->get('flag', ''), ['1', '2'])) {
            $model = $model->where('flag', $flag);
        }

        if (in_array($flag = $this->get('focus', ''), ['1', '2'])) {
            $model = $model->where('focus', $flag);
        }

        if (($start_time = strtotime(trim($this->get('start_time', '')))) && ($end_time = strtotime(trim($this->get('end_time', ''))))) {
            $model = $model->whereBetween('match_time', [$start_time, $end_time]);
        } else if ($start_time) {
            $model = $model->whereBetween('match_time', [$start_time, time()]);
        }

        if ($home = trim($this->get('home', ''))) {
            $model = $model->whereLike('home', $home . "%");
        }

        if ($away = trim($this->get('away', ''))) {
            $model = $model->whereLike('away', $away . "%");
        }

        return json([
            "count" => $model->count('id'),
            "data"  => $model->order('match_time', 'desc')
                ->page($this->get('page', 1), $this->get('limit', 50))
                ->select(),
            "code"  => 0,
            "msg"   => ""
        ]);
    }

    public function set_flag()
    {
        $id    = Request::param("id");
        $flag  = Request::param("flag");
        $query = MatchesScheduleModel::where(["id" => $id])->update(["flag" => $flag]);
        if ($query) {
            // 记录日志
            $log_content = "设置重要赛事，比赛ID：{$id}";
            parent::addAdminLog($log_content);
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    /**
     * 设置为焦点赛事
     * @return \think\response\Json
     */
    public function set_focus()
    {
        $id    = Request::param("id");
        $focus = Request::param("focus");
        $query = MatchesScheduleModel::where(["id" => $id])->update(["focus" => $focus]);
        if ($query) {
            // 记录日志
            $log_text    = $focus == 2 ? '焦点赛事' : '普通赛事';
            $log_content = "设置重要赛事，设置为:{$log_text}，比赛ID：{$id}";
            parent::addAdminLog($log_content);
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    public function set_ids_flag()
    {
        $put_ids    = Request::param("put_ids");
        $where[] = ["id", "in", $put_ids];
        if ($put_ids != '') {
            $query = MatchesScheduleModel::where($where)->update(["flag" => 2]);
            if ($query) {
                // 记录日志
                $log_content = "设置重要赛事，比赛ID：{$put_ids}";
                parent::addAdminLog($log_content);
                return self::bulidSuccess();
            }
        }
        return self::bulidFail();
    }

    public function cancel_ids_flag()
    {
        $put_ids    = Request::param("put_ids");
        $where[] = ["id", "in", $put_ids];
        if ($put_ids != '') {
            $query = MatchesScheduleModel::where($where)->update(["flag" => 1]);
            if ($query) {
                // 记录日志
                $log_content = "设置重要赛事，比赛ID：{$put_ids}";
                parent::addAdminLog($log_content);
                return self::bulidSuccess();
            }
        }
        return self::bulidFail();
    }
}
